//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model
{
	import __AS3__.vec.Vector;
	
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.model.vo.BlogFetchVO;
	import reader.model.vo.BlogItemComboBoxDataVO;
	import reader.model.vo.BlogLabelVO;
	import reader.model.vo.BlogSourceVO;
	
	public class BaseDataProxy extends Proxy implements IBaseDataProxy
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "BaseDataProxy";
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var blogItemComboBoxData:BlogItemComboBoxDataVO = new BlogItemComboBoxDataVO();
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		private var readerResource:ReaderResource = ReaderResource.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function BaseDataProxy(proxyName:String=null)
		{
			super((proxyName != null)?proxyName:NAME, []);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods

		public function creatLabelTree():XML
		{
			var labelArray	:Vector.<BlogLabelVO> = readerModelLocator.blogLabels;
			var blogSource	:Vector.<BlogSourceVO> = readerModelLocator.blogSource;
			var isHideUnRead:Boolean = readerModelLocator.user.isLabelNew;
			
			var treeXML:XML = <root type="folder" label={ReaderResource.instance.resource.labelTreeRootItemLabel}></root>;
			var itemXml:XML;				
			var bs:BlogSourceVO;
			
			//if labels in database not null, creat folder node and add items into it.
			if(labelArray)
			{
				//creat root label folder.
				for each(var bl:BlogLabelVO in labelArray)
				{
					var folderXml:XML = <source id={bl.labelid} label={bl.label} type="folder" newBlogs="0" isBranch="true"/> ;
					treeXML.appendChild(folderXml);
				}
				
				//initialize a dictionary for new blogs computing.
				var foldDict:Dictionary = new Dictionary();
				for each(bs in blogSource)
				{
					foldDict[bs.labelId] = 0;
				}
				bs = null;
				for each(bs in blogSource)
				{
					//if has label, add to folder, else add to root node.
					if(bs.labelId != -1)
					{
						itemXml = <source id={bs.blogSourceId} label={bs.label} labelId={bs.labelId} newBlogs={bs.newBlogs} type="item" />;
						foldDict[bs.labelId] += bs.newBlogs;
													
						var foldList:XMLList = treeXML.source.(@id == bs.labelId);
						foldList[0].@newBlogs = foldDict[bs.labelId];
						foldList[0].appendChild(itemXml);
					}
					else
					{
						itemXml = <source id={bs.blogSourceId} label={bs.label} labelId={bs.labelId} newBlogs={bs.newBlogs} type="item"/>;
						treeXML.appendChild(itemXml);
					}
					
					//labelTotal repersent a reference for total noew blogs for each label.
					readerModelLocator.labelTotal[bs.labelId] = foldDict[bs.labelId];
				}
				bs = null;
			}
			else
			{	
				//there are no labels(labelId = -1), just add items to root node.
				for each(bs in blogSource)
				{
					itemXml = <source id={bs.blogSourceId} label={bs.label} labelId={bs.labelId} newBlogs={bs.newBlogs} type="item"/>;
					treeXML.appendChild(itemXml);
				}
			}
			
			//hide fold and items that have no new blogs.
			//TODO: need to improved...
			var i:int;
			if(isHideUnRead)
			{
				var xl:XMLList = treeXML.source;
				for(i = 0; i < xl.length(); i++)
				{
					//delete root nodes.
					if(xl[i].@newBlogs <= 0)
					{
						delete xl[i];
						i--;
					}
					var xi:XMLList = xl[i].source;
					if(xi && xi.length() > 0)
					{
						for(var j:int = 0; j < xi.length(); j++)
						{
							//delete nodes in folder.
							if(xi[j].@newBlogs <= 0)
							{
								delete xi[j];
								j--;
							}
						}
					}
					
				}
			}
			
			return treeXML;
		}
				
		public function creatBlogItemComboBoxData(isDesc:Boolean, blogFetch:BlogFetchVO, isNullData:Boolean):Array
		{
			var promot:Object = blogItemComboBoxData.promot;
			
			if(!blogFetch)
			{
				return [promot];
			}
			
			var sep:Object = blogItemComboBoxData.sep;			
			var desc:Object = blogItemComboBoxData.desc;
			desc.icon = isDesc ? readerResource.RssStatusOK :readerResource.RssStatusNull;			
			var asc:Object = blogItemComboBoxData.asc;
			asc.icon = isDesc ? readerResource.RssStatusNull :readerResource.RssStatusOK;
			
			if(blogFetch.blogsource.blogSourceId < 0)
			{
				return isNullData ? [promot] : [promot, desc, asc];
			}
			
			var deSub:Object = blogItemComboBoxData.deSub;			
			var reName:Object = blogItemComboBoxData.reName;			
			var modefy:Object = blogItemComboBoxData.modefy;			
			var creat:Object = blogItemComboBoxData.creat;
			
			var blogItemComboBoxList:Array = isNullData ? [promot, sep, deSub, reName, sep, modefy] : [promot, desc, asc, sep, deSub, reName, sep, modefy];
			
			if(readerModelLocator.blogLabels.length <= 0)
			{
				return blogItemComboBoxList;
			}
					
			var a:Array = [];
			for each(var bl:BlogLabelVO in readerModelLocator.blogLabels)
			{
				var folder:Object = {enable: true, icon:readerResource.RssStatusNull};
				folder.label = bl.name;
				folder.data = bl.labelid;
				if(bl.labelid == blogFetch.blogsource.labelId)
					folder.icon = readerResource.RssStatusOK;
				a.push(folder);
			}
			a.push(creat);
			return blogItemComboBoxList.concat(a);
		}
	}
}
